#include "VSAnimFunction.h"
#include "VSBoneNode.h"
#include "VSGraphicInclude.h"
#include "VSStream.h"
using namespace VSEngine2;
IMPLEMENT_RTTI_NoCreateFun(VSAnimFunction,VSAnimBaseFunction)
VSAnimFunction::VSAnimFunction()
{
	m_BoneOutPut.Clear();

}
VSAnimFunction::VSAnimFunction(const VSUsedName & ShowName,VSAnimTree * pAnimTree)
:VSAnimBaseFunction(ShowName,pAnimTree)
{
	const VSSkelectonMeshNode* pMesh = GetSkelectonMeshNode(); 
	VSMAC_ASSERT(pMesh);

	VSSkelecton * pSkelecton = pMesh->GetSkelecton();
	VSMAC_ASSERT(pSkelecton);

	unsigned int BoneNum = GetSkelectonMeshNode()->GetSkelecton()->GetBoneNum();
	m_BoneOutPut.SetBufferNum(BoneNum);
	for(unsigned int i = 0 ; i < m_BoneOutPut.GetNum() ; i++)
	{
		m_BoneOutPut[i].Identity();

	}
}
VSAnimFunction::~VSAnimFunction()
{
	m_BoneOutPut.Clear();
}
unsigned int VSAnimFunction::DiskUse()const
{
	unsigned int Used = VSAnimBaseFunction::DiskUse();
	Used += sizeof(unsigned int);
	return Used;
}
bool VSAnimFunction::Save(VSStream & rStream,unsigned int &iSaveUse)const
{
	if(!VSAnimBaseFunction::Save(rStream,iSaveUse))
		return 0;
	unsigned int uiBoneOutPutNum = m_BoneOutPut.GetNum();
	if(!rStream.Write(&uiBoneOutPutNum,sizeof(unsigned int)))
		return 0;
	iSaveUse += sizeof(unsigned int);
	
	return 1;
}
bool VSAnimFunction::Load(VSStream & rStream,unsigned int &iSaveUse)
{	
	if(!VSAnimBaseFunction::Load(rStream,iSaveUse))
		return 0;
	unsigned int uiBoneOutPutNum;
	if(!rStream.Read(&uiBoneOutPutNum,sizeof(unsigned int)))
		return 0;
	iSaveUse += sizeof(unsigned int);
	
	m_BoneOutPut.SetBufferNum(uiBoneOutPutNum);
	for(unsigned int i = 0 ; i < m_BoneOutPut.GetNum() ; i++)
	{
		m_BoneOutPut[i].Identity();

	}

	return 1;
}
bool VSAnimFunction::Clone(const VSObject *pObject,unsigned int uiType)
{
	const VSAnimFunction* Temp = DynamicCast<VSAnimFunction>(pObject); 
	if(!Temp)
		return 0;
	if(VSAnimBaseFunction::Clone(pObject,uiType))
	{
		m_BoneOutPut = Temp->m_BoneOutPut;
		return 1;
	}
	else
		return 0;
}
bool VSAnimFunction::UpdateEx(double dAppTime)
{
	if(!VSAnimBaseFunction::UpdateEx(dAppTime))
		return false;
	for(unsigned int i = 0 ; i < m_BoneOutPut.GetNum() ; i++)
	{
		m_BoneOutPut[i].Identity();

	}

	for (unsigned int i = 0 ; i < m_pInput.GetNum() ;i++)
	{
		if(m_pInput[i]->GetOutputLink())
		{
			VSAnimBaseFunction *pAnimBaseFunction = (VSAnimBaseFunction *)m_pInput[i]->GetOutputLink()->GetOwner();
			if(pAnimBaseFunction)
			{
				pAnimBaseFunction->Update(dAppTime);
			}
		}
	}
	return true;

}